﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using HueLampStatusDetector.PhilipsHue;
using HueLampStatusDetector.StatusChecker;

namespace HueLampStatusDetector.Status
{
    public class StatusManager
    {
        private object padLock = new object();
        private int _currentBusyNotifications;
        private HueControl _hueControl;

        public StatusManager(List<IStatusChecker> statusList)
        {
            if (statusList == null)
            {
                throw new ArgumentNullException("statusList");
            }

            _hueControl = new HueControl();
            statusList.ForEach(x => x.ChangeStatus += (sender, args) => ChangeBusyStatus(args.StatusType));
        }

        private void ChangeBusyStatus(StatusType statusType)
        {
            lock (padLock)
            {
                _currentBusyNotifications += statusType == StatusType.Free
                    ? -1
                    : 1;
                Trace.TraceInformation("Busy count: {0}", _currentBusyNotifications);

                if (_currentBusyNotifications == 0)
                {
                    _hueControl.SetColor(Color.Green);
                }
                else if (_currentBusyNotifications == 1)
                {
                    _hueControl.SetColor(Color.Red);
                }
            }
        }
    }
}
